package com.ctshk.rpc.goods.mapper.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.goods.dto.ShoppingShopStoreDTO;
import com.ctshk.rpc.goods.req.ShoppingShopStorePageReq;

/**
 * 小店管理列表查询
 * @author 姜斌文
 * @since 2021-04-06
 */
public class ShoppingShopStoreProvider {

    public String findShoppingShopStoreByCondition(IPage<ShoppingShopStoreDTO> page,ShoppingShopStorePageReq pageReq) {
    	String sql = new SQL() {{
                SELECT("t.id,t.store_invitation_code,t.head_portrait_url,t.store_name,t.staff_id, t.staff_name, t.shop_id, t.shop_name,t.shop_type,t.status,t.gmt_create,"
                		+ "(SELECT COUNT(1) + 1 FROM t_shopping_shop_store s WHERE s.store_id = t.id) AS \"totalMemberNumber\"");
                FROM("t_shopping_shop_store t");
                if (StringUtils.isNotBlank(pageReq.getStoreNameOrstaffName())) {
                    WHERE("(t.staff_name like concat('%',#{pageReq.storeNameOrstaffName},'%')) OR (t.store_name like concat('%',#{pageReq.storeNameOrstaffName},'%'))");
                }
                if (StringUtils.isNotBlank(pageReq.getShopName())){
                    WHERE("(t.shop_name like concat('%', #{pageReq.shopName}, '%'))");
                }
                if (null != pageReq.getShopId()) {
                    WHERE("t.shop_id = #{pageReq.shopId}");
                }
                if (null != pageReq.getUserIds()){
                    WHERE("t.create_id in (${pageReq.userIds})");
                }
                ORDER_BY("t.gmt_create DESC");
            }}.toString();
            return sql;
    }
}
